Skip to content

Method: verifyStatementsPresent(Collection, EntityManager)

1: /**
2: * Copyright (C) 2019 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.runner;
14:
15: import cz.cvut.kbss.jopa.model.EntityManager;
16: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
17: import cz.cvut.kbss.jopa.test.*;
18: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
19: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
20: import cz.cvut.kbss.jopa.test.environment.Triple;
21: import org.junit.jupiter.api.AfterEach;
22: import org.slf4j.Logger;
23:
24: import java.net.URI;
25: import java.util.*;
26:
27: import static org.junit.jupiter.api.Assertions.*;
28:
29:
30: public abstract class BaseRunner {
31:
32: protected static final URI CONTEXT_ONE = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#One");
33: protected static final URI CONTEXT_TWO = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#Two");
34:
35:
36: protected final Logger logger;
37:
38: protected EntityManager em;
39:
40: protected OWLClassA entityA;
41: protected OWLClassB entityB;
42: protected OWLClassC entityC;
43: protected OWLClassD entityD;
44: // Generated IRI
45: protected OWLClassE entityE;
46: // Two relationships, cascade
47: protected OWLClassG entityG;
48: protected OWLClassH entityH;
49: // Lazy reference to OWLClassA
50: protected OWLClassI entityI;
51: protected OWLClassM entityM;
52: protected OWLClassN entityN;
53: protected OWLClassP entityP;
54: // Mapped superclass
55: protected OWLClassQ entityQ;
56:
57: protected final DataAccessor dataAccessor;
58: protected final PersistenceFactory persistenceFactory;
59:
60: public BaseRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
61: assert logger != null;
62: this.logger = logger;
63: this.persistenceFactory = persistenceFactory;
64: this.dataAccessor = dataAccessor;
65: init();
66: }
67:
68: /**
69: * Initializes the test entities in the following manner:
70: * <pre>
71: * <ul>
72: * <li>entityA contains non-empty types</li>
73: * <li>entityB's properties are null</li>
74: * <li>entityC's simple and referenced lists are null</li>
75: * <li>entityD's reference to OWLClassA is set to entityA</li>
76: * <li>entityE's URI is left null for ID generation</li>
77: * <li>entityG's reference to OWLClassH is set to entityH</li>
78: * <li>entityH's reference to OWLClassA is set to entityA</li>
79: * <li>entityI's reference to OWLClassA is set to entityA</li>
80: * <li>entityM has all fields set to some values</li>
81: * <li>entityN has required fields set</li>
82: * <li>entityP's properties and uri are null</li>
83: * <li>entityQ's reference to OWLClassA is set to entityA</li>
84: * </ul>
85: * </pre>
86: */
87: private void init() {
88: this.entityA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
89: entityA.setStringAttribute("entityAStringAttribute");
90: final Set<String> types = new HashSet<>();
91: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassU");
92: entityA.setTypes(types);
93: this.entityB = new OWLClassB();
94: entityB.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB"));
95: entityB.setStringAttribute("entityBStringAttribute");
96: this.entityC = new OWLClassC();
97: entityC.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityC"));
98: this.entityD = new OWLClassD(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD"));
99: entityD.setOwlClassA(entityA);
100: this.entityE = new OWLClassE();
101: entityE.setStringAttribute("entityEStringAttribute");
102: this.entityH = new OWLClassH();
103: entityH.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityH"));
104: entityH.setOwlClassA(entityA);
105: this.entityG = new OWLClassG();
106: entityG.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
107: entityG.setOwlClassH(entityH);
108: this.entityI = new OWLClassI();
109: entityI.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityI"));
110: entityI.setOwlClassA(entityA);
111: this.entityM = new OWLClassM();
112: entityM.initializeTestValues(true);
113: this.entityN = new OWLClassN();
114: entityN.setStringAttribute("entityNStringAttribute");
115: this.entityP = new OWLClassP();
116: this.entityQ = new OWLClassQ();
117: entityQ.setStringAttribute("entityQStringAttribute");
118: entityQ.setParentString("entityQParentStringAttribute");
119: entityQ.setLabel("entityQLabel");
120: entityQ.setOwlClassA(entityA);
121: }
122:
123: @AfterEach
124: public void tearDown() {
125: if (em != null && em.isOpen()) {
126: if (em.getTransaction().isActive()) {
127: em.getTransaction().rollback();
128: }
129: em.close();
130: em.getEntityManagerFactory().close();
131: }
132: }
133:
134: /**
135: * Persists the specified instance(s) in a separate transaction.
136: *
137: * @param entity Entity to persist
138: */
139: protected void persist(Object... entity) {
140: em.getTransaction().begin();
141: for (Object ent : entity) {
142: em.persist(ent);
143: }
144: em.getTransaction().commit();
145: }
146:
147: /**
148: * Runs the specified action in a transaction on the current entity manager.
149: *
150: * @param action The code to run
151: */
152: protected void transactional(Runnable action) {
153: em.getTransaction().begin();
154: action.run();
155: em.getTransaction().commit();
156: }
157:
158: /**
159: * Verifies that no statements with the specified individual as subject exist in the ontology any more.
160: *
161: * @param identifier Individual identifier
162: */
163: protected void verifyIndividualWasRemoved(URI identifier) {
164: // TODO There is a bug in OWL2Query - the query returns true, because it finds the top object and data property assertion for an individual
165: // which doesn't exist anymore (but is a part of the query)
166: final boolean remains = em.createNativeQuery("ASK WHERE { ?instance ?y ?z . }", Boolean.class)
167: .setParameter("instance", identifier).getSingleResult();
168: assertFalse(remains);
169: }
170:
171: protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled) {
172: return getEntityManager(repositoryName, cacheEnabled, Collections.emptyMap());
173: }
174:
175: protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled,
176: Map<String, String> properties) {
177: return persistenceFactory.getEntityManager(repositoryName, cacheEnabled, properties);
178: }
179:
180: protected void persistTestData(Collection<Triple> data, EntityManager em) throws Exception {
181: dataAccessor.persistTestData(data, em);
182: }
183:
184: protected void verifyStatementsPresent(Collection<Triple> expected, EntityManager em) throws Exception {
185: dataAccessor.verifyDataPresent(expected, em);
186: }
187:
188: protected void verifyStatementsNotPresent(Collection<Triple> notExpected, EntityManager em) throws Exception {
189: dataAccessor.verifyDataNotPresent(notExpected, em);
190: }
191:
192: // Utility methods to reduce duplication
193:
194: /**
195: * Persists the specified instance of {@link OWLClassC} together will all items in the lists (if specified).
196: *
197: * @param instance Instace to persist
198: */
199: void persistCWithLists(OWLClassC instance) {
200: transactional(() -> {
201: em.persist(instance);
202: if (instance.getSimpleList() != null) {
203: instance.getSimpleList().forEach(em::persist);
204: }
205: if (instance.getReferencedList() != null) {
206: instance.getReferencedList().forEach(em::persist);
207: }
208: });
209: }
210:
211: /**
212: * Finds instance and checks for it not being {@code null}.
213: *
214: * @return The found instance
215: */
216: <T> T findRequired(Class<T> type, Object identifier) {
217: final T result = em.find(type, identifier);
218: assertNotNull(result);
219: return result;
220: }
221:
222: /**
223: * Finds instance and checks for it not being {@code null}.
224: *
225: * @return The found instance
226: */
227: <T> T findRequired(Class<T> type, Object identifier, Descriptor descriptor) {
228: final T result = em.find(type, identifier, descriptor);
229: assertNotNull(result);
230: return result;
231: }
232:
233: <T> void verifyExists(Class<T> type, Object identifier) {
234: assertNotNull(em.find(type, identifier));
235: }
236:
237: protected void verifyValueDatatype(URI identifier, String property, String expectedDatatype) {
238: assertTrue(em.createNativeQuery("ASK WHERE { ?x ?property ?value . " +
239: "FILTER (DATATYPE(?value) = ?datatype) }", Boolean.class)
240: .setParameter("x", identifier)
241: .setParameter("property", URI.create(property))
242: .setParameter("datatype", URI.create(expectedDatatype)).getSingleResult());
243: }
244: }